package com.ailanyin.modules.flowable.controller;

import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.modules.flowable.domain.WfDeployForm;
import com.ailanyin.modules.flowable.domain.bo.WfFormBo;
import com.ailanyin.modules.flowable.domain.vo.WfFormVo;
import com.ailanyin.modules.flowable.service.IWfDeployFormService;
import com.ailanyin.modules.flowable.service.IWfFormService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 流程表单Controller
 *
 * @author KonBAI
 * @createTime 2022/3/7 22:07
 */
@RestController
@RequestMapping("/workflow/form")
public class WfFormController {

    @Autowired
    private IWfFormService formService;

    @Autowired
    private IWfDeployFormService deployFormService;


    @GetMapping("/list")
    public CommonPage<WfFormVo> list(WfFormBo bo,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<WfFormVo> list = formService.selectList(bo);
        return CommonPage.restPage(list);
    }


    /**
     * 获取流程表单详细信息
     *
     * @param formId 主键
     */
    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId) {
        return AjaxResult.success(formService.queryById(formId));
    }

    /**
     * 新增流程表单
     */
    @PostMapping
    public AjaxResult add(@RequestBody WfFormBo bo) {
        return AjaxResult.toAjax(formService.insertForm(bo));
    }

    /**
     * 修改流程表单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WfFormBo bo) {
        return AjaxResult.toAjax(formService.updateForm(bo));
    }

    /**
     * 删除流程表单
     *
     * @param formIds 主键串
     */
    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds) {
        return AjaxResult.toAjax(formService.deleteByIds(Arrays.asList(formIds)));
    }


    /**
     * 挂载流程表单
     */
    @PostMapping("/addDeployForm")
    public AjaxResult addDeployForm(@RequestBody WfDeployForm deployForm) {
        return AjaxResult.toAjax(deployFormService.insertWfDeployForm(deployForm));
    }
}
